﻿using System;
using System.Linq;

namespace Basmus.Values
{
    public class CompositeBuilder<T> : IBuilder<T>
    {
        private readonly Func<T, bool> _predicate;
        private readonly IBuilder<T>[] _builders;

        public CompositeBuilder(Func<T, bool> predicate, params IBuilder<T>[] builders)
        {
            _predicate = predicate;
            _builders = builders;
        }

        public T Build(ValuesContext valuesContext)
        {
            var target = _builders
                .Select(x => x.Build(valuesContext))
                .FirstOrDefault(_predicate);
            return target;
        }
    }
}